package com.pig4cloud.pig.demo.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.demo.Mapper.CollectionMapper;
import com.pig4cloud.pig.demo.Service.CollectionService;
import com.pig4cloud.pig.demo.feign.Entity.Collection;
import com.pig4cloud.pig.demo.feign.Entity.CollectionVO;
import com.pig4cloud.pig.demo.feign.vo.ArticleCollectionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {
    @Autowired
    private CollectionMapper collectionMapper;

    @Override
    public int insert(Collection collection) {
        return collectionMapper.insert(collection);
    }

    @Override
    public Collection findByUidAndAid(Long aid, Long uid) {
        QueryWrapper<Collection> wrapper = new QueryWrapper();

        if (aid != null && uid != null) {
            wrapper.eq("aid", aid);
            wrapper.eq("uid", uid);

            Collection collection = collectionMapper.selectOne(wrapper);

            if (collection != null)
                return collection;

        }

        return null;
    }

    @Override
    public CollectionVO getByUidAndAid(Long aid, Long uid) {
        Collection collection = findByUidAndAid(aid, uid);

        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        if (aid != null)
            wrapper.eq("aid", aid);
        Integer count = collectionMapper.selectCount(wrapper);
        if (collection == null)
            return new CollectionVO(false, count);
        return new CollectionVO(true, count);
    }

    @Override
    public CollectionVO updateCollectionById(Long aid, Long uid) {
//        Collection collection = findByUidAndAid(aid, uid);
//
//        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
//        if (aid != null)
//            wrapper.eq("aid", aid);
//        Integer count = collectionMapper.selectCount(wrapper);

        CollectionVO collectionVO = getByUidAndAid(aid, uid);
        if (collectionVO.isCollection()) {
            QueryWrapper<Collection> wrapper = new QueryWrapper<>();
            wrapper.eq("aid", aid);
            wrapper.eq("uid", uid);
            int delete = collectionMapper.delete(wrapper);
            if (delete > 0)
                return new CollectionVO(false, collectionVO.getCollectionNum() - 1);
            else
                return new CollectionVO(false, collectionVO.getCollectionNum());
        } else {
            Collection co = new Collection();
            co.setAid(aid);
            co.setUid(uid);
            int insert = insert(co);

            if (insert > 0)
                return new CollectionVO(true, collectionVO.getCollectionNum() + 1);
            else
                return new CollectionVO(true, collectionVO.getCollectionNum());
        }

//        if (collection == null) {
//            Collection co = new Collection();
//            co.setAid(aid);
//            co.setUid(uid);
//            int insert = insert(co);
//            if (insert == 0)
//                return null;
//            else
//                return new CollectionVO(true, count + 1);
//        } else {
//            int i = collectionMapper.deleteById(collection.getId());
//            if (i == 0)
//                return null;
//            else
//                return new CollectionVO(false, count - 1);
//        }
    }

    @Override
    public IPage<ArticleCollectionVO> queryArticleCollectionVO(Page<?> page, Long id) {
        return collectionMapper.queryArticleCollectionVO(page, id);
    }
}
